<?php
if(!defined('IN_BSTO')){ die('[ERROR] You cannot load this page directly !!!'); }

class User extends BaseElement
{
	private $fullname = '';
	private $avatar = '';
	private $fbid = '';
	private $datejoin = '';
	private $uid ='';	
	private $username = '';
	private $password = '';
	private $email = '';
	private $phone = '';
	private $company ='';
	private $address ='';
	private $type ='';
	private $status ='';
	
	function __construct($db)
    {
        $this->db = $db;        
    }

	public function init($username, $password, $email, $phone='', $company='', $address='', $status=0, $type='Normal', $fbid='', $fullname='', $avatar='') {
		$this->username = $username;
		$this->password = hash('sha256', $password);
		$this->email = $email;
		$this->phone = $phone;
		$this->company = $company;
		$this->address = $address;
		$this->status = $status;
		$this->type = $type;
		$this->fbid = $fbid;
		$this->fullname = $fullname;
		$this->avatar = $avatar;

	}
	public function initWithoutHash($username, $password, $email, $phone='', $company='', $address='', $status=0, $type='Normal', $fbid='', $fullname='', $avatar='') {
		$this->username = $username;
		$this->password = $password;
		$this->email = $email;
		$this->phone = $phone;
		$this->company = $company;
		$this->address = $address;
		$this->status = $status;
		$this->type = $type;
		$this->fbid = $fbid;
		$this->fullname = $fullname;
		$this->avatar = $avatar;

	}
 	public function set($name, $value)
    {
      return $this->$name = $value;
    }
    
    public function get($name)
    {
        return $this->$name;
    }
	public function checkPassword($password)
	{
		return $this->password == hash('sha256', $password); 
	} 
	public function changePassword($newpass)
	{
		$this->password = hash('sha256', $newpass);
	}

    public function save()
    {    	
    	if ($this->db != null) {
    		$this->db->startConnection();
	        if ($this->uid != "") {	        	
	        	$this->db->dbQuery("UPDATE user SET uid = 	   ".var_export($this->uid, true).",
	        										username = ".var_export($this->username, true).",
	        										password = ".var_export($this->password, true).",
	        										email = ".var_export($this->email, true).",
	        										phone    = ".var_export($this->phone,true)    .",
	        										company  = ".var_export($this->company,true)  .",
	        										address  = ".var_export($this->address,true)  .",
	        										type    = ".var_export($this->type,true)  .",
	        										status   = ".var_export($this->status,true)  .",
	        										fbid   = ".var_export($this->fbid,true)  .",
	        										fullname   = ".var_export($this->fullname,true)  .",
	        										avatar   = ".var_export($this->avatar,true)  ."
	        				WHERE uid = ".var_export($this->uid, true).";");
	        }
	        else {
	        	$this->db->dbQuery("INSERT INTO user (username, password, email,phone,company,address,status,type,fbid,fullname,avatar)
	        						VALUES(".var_export($this->username, true).","
	        								.var_export($this->password, true).","
	        								.var_export($this->email, true).","
	        								.var_export($this->phone, true).","
	        								.var_export($this->company, true).","
	        								.var_export($this->address, true).","
	        								.var_export($this->status, true).","
	        								.var_export($this->type, true).","
	        								.var_export($this->fbid, true).","
	        								.var_export($this->fullname, true).","
	        								.var_export($this->avatar, true).");");
	        }
	        $this->db->closeConnection();
    	}
    	else {
    		echo "Error: Connect to DataBase";
    	}
    }
    public function delete()
    {

    	if ($this->db != null) {
    		//$this->db->startConnection();
        	$db1->dbQuery("DELETE FROM user WHERE uid=".var_export($this->uid,true).";");
        	//$this->db->closeConnection();
    	}
    }
    public function __destruct() {

    	if ($this->db != null) {

    	}
    }
    
    public function toArray()
    {
    	return array(
    		'uid' => $this->uid,
    		'username' => $this->username,
    		'email' => $this->email,
    		'phone' => $this->phone,
    		'company' => $this->company,
    		'status' => $this->status,
    		'type' => $this->type,
    	);
    }
}
?>